// javamaps
// Copyright (c) 2010 Lup Gratian
package Rendering.Renderers;
import Core.*;
import Rendering.Utils.*;
import java.awt.Image;
import java.util.ArrayList;

public class ImageCache {
    private static int MAX_IMAGES = 128;

    /*
     * Membrii.
     */
    private ArrayList<Cache<Image>> cache_;

    /*
     * Constructori.
     */
    public ImageCache(IMapProvider provider) {
        int zoomLevels = provider.ZoomLevels();
        cache_ = new ArrayList<Cache<Image>>(2);

        // Primul nivel de zoom este tinut in totalitate in cache.
        cache_.add(new Cache<Image>(provider.TileCount(0)));
        cache_.add(new Cache<Image>(MAX_IMAGES - provider.TileCount(0)));
    }

    /*
     * Metode publice.
     */
    public void Add(Image image, ObjectId id, int zoom) {
        cache_.get(Select(zoom)).Add(image, id);
    }

    public Image Get(ObjectId id, int zoom) {
        return cache_.get(Select(zoom)).Get(id);
    }

    public boolean Contains(ObjectId id, int zoom) {
        return cache_.get(Select(zoom)).Contains(id);
    }

    public void Remove(ObjectId id, int zoom) {
        cache_.get(Select(zoom)).Remove(id);
    }

    public void Clear(int zoom) {
        cache_.get(Select(zoom)).Clear();
    }

    public void Clear() {
        for(int i = 0; i < cache_.size(); i++) {
            cache_.get(i).Clear();
        }
    }

    public int Capacity(int zoom) {
        return cache_.get(Select(zoom)).Capacity();
    }

    public int Capacity() {
        int ct = 0;
        for(int i = 0; i < cache_.size(); i++) {
            ct += cache_.get(i).Capacity();
        }

        return ct;
    }

    public int Count() {
        int ct = 0;
        for(int i = 0; i < cache_.size(); i++) {
            ct += cache_.get(i).Count();
        }

        return ct;
    }

    /*
     * Metode private.
     */
    private int Select(int zoom) {
        if(zoom == 0) return 0;
        else return 1;
    }
}